package com.atguigu.guli.service.eduucenter.controller;


import com.atguigu.guli.service.base.result.R;
import com.atguigu.guli.service.eduucenter.entity.RegisterVo;
import com.atguigu.guli.service.eduucenter.entity.UcenterMember;
import com.atguigu.guli.service.eduucenter.service.UcenterMemberService;
import com.atguigu.guli.service.utils.JwtUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 会员表 前端控制器
 * </p>
 *
 * @author atguigu
 * @since 2022-06-12
 */
@RestController
@CrossOrigin
@RequestMapping("/eduucenter/ucentermember")
public class UcenterMemberController {
  @Autowired
    private UcenterMemberService ucenterMemberService;

    //登录
    @PostMapping ("login")
    //requestbody 用get提交有问题
    public R login(@RequestBody UcenterMember ucenterMember){
        //返回token值
        String token = ucenterMemberService.login(ucenterMember);
        return R.ok().data("token",token);
    }

    @PostMapping("register")
    public R register(@RequestBody RegisterVo registerVo){

        ucenterMemberService.register(registerVo);
        return R.ok();
    }

    @GetMapping("getMemberInfo")
    public R getMemberInfo(HttpServletRequest request){
        String idByJwtToken = JwtUtils.getMemberIdByJwtToken(request);
        UcenterMember member = ucenterMemberService.getById(idByJwtToken);
        return R.ok().data("userInfo",member);
    }

}

